package com.mav.controller;

import com.mav.pojo.Dept;
import com.mav.pojo.Result;
import com.mav.service.DeptService;
import com.mav.service.iml.DeleteException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    DeptService deptService;
    @GetMapping
    public Result list(){
        log.info("查找全部部门");
        List<Dept> list=deptService.findAll();
        return Result.success(list);
    }
    @DeleteMapping
    public Result delete(Integer id) throws DeleteException {
        log.info("按照id删除,id:{}",id);
        deptService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("按照id查询部门,id:{}",id);

        return Result.success(deptService.getInfo(id));
    }
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("更新部门数据 {}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
